package com.yt.lxh.activity.config;

import com.yt.lxh.constant.MQConst;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 优惠券发布的通知配置
 */
@Configuration
public class CouponPublishNotifyConfig {

    // 声明延迟发布优惠券交换机
    @Bean(MQConst.COUPON_PUBLISH_DELAYED_EXCHANGE)
    public CustomExchange couponPublishDelayedExchange() {
        Map<String, Object> arg = new HashMap<>();
        arg.put("x-delayed-type", "direct");
        return new CustomExchange(
                MQConst.COUPON_PUBLISH_DELAYED_EXCHANGE,
                "x-delayed-message",
                true,
                false,
                arg);
    }

    // 声明优惠券延迟发布队列，且持久化
    @Bean(MQConst.COUPON_PUBLISH_DELAYED_QUEUE)
    public Queue couponPublishDelayedQueue() {
        return QueueBuilder.durable(MQConst.COUPON_PUBLISH_DELAYED_QUEUE).build();
    }

    // 声明优惠券延迟发布队列与交换机绑定关系
    @Bean
    public Binding couponPublishDelayedBinding(
            @Qualifier(MQConst.COUPON_PUBLISH_DELAYED_EXCHANGE) Exchange exchange,
            @Qualifier(MQConst.COUPON_PUBLISH_DELAYED_QUEUE) Queue queue
    ) {
        return BindingBuilder.bind(queue).to(exchange).with(MQConst.COUPON_PUBLISH_KEY).noargs();
    }

}
